/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Payment.java
 *
 * Created on 28-nov-2011, 14:24:22
 */
package view;

import java.util.List;
import main.WinkelApplication;
import model.Customer;

/**
 *
 * @author Bryan
 */
public class Payment extends javax.swing.JPanel {
    private Customer customer;
    private int customer_id;
    
    /** Creates new form Payment */
    public Payment() {
        initComponents();
        int teller = 0;
        
        this.setSize(446, 263);
        this.setPreferredSize(this.getSize());
        this.setMinimumSize(this.getSize());
        this.setMaximumSize(this.getSize());
        
        List<String> paymentmethods = WinkelApplication.getQueryManager().getPaymentMethods();
        for(String paymentmethod: paymentmethods) {
            cmbPaymentmethod.insertItemAt(paymentmethod, teller);
            teller++;
        }
        cmbPaymentmethod.setSelectedIndex(0);
    }
    
    public Payment(int customer_id) {
        initComponents();
        int teller = 0;
        
        /*cmbPaymentmethod.removeAll();
        
        List<String> paymentmethods = WinkelApplication.getQueryManager().getPaymentMethods();
        for(String paymentmethod: paymentmethods) {
            cmbPaymentmethod.add(paymentmethod);
        }*/
        
        customer = WinkelApplication.getQueryManager().getCustomer(customer_id);
        txtCustomerId.setText(String.valueOf(customer.getCustomerId()));
        txtName.setText(customer.getName());
        txtAddress.setText(customer.getAddress());
        txtZipcode.setText(customer.getZipcode());
        txtCity.setText(customer.getCity());
        
        List<String> paymentmethods = WinkelApplication.getQueryManager().getPaymentMethods();
        for(String paymentmethod: paymentmethods) {
            cmbPaymentmethod.insertItemAt(paymentmethod, teller);
            teller++;
        }
        cmbPaymentmethod.setSelectedIndex(0);        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtCustomerId = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        txtZipcode = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        txtCity = new javax.swing.JTextField();
        btnSend = new javax.swing.JButton();
        btnSelectCustomerId = new javax.swing.JButton();
        lblCustomerID = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblZipcode = new javax.swing.JLabel();
        lblCity = new javax.swing.JLabel();
        cmbPaymentmethod = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtNote = new javax.swing.JTextArea();
        lblNote = new javax.swing.JLabel();
        btnBack = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(446, 263));
        setMinimumSize(new java.awt.Dimension(446, 263));
        setPreferredSize(new java.awt.Dimension(446, 263));

        txtCustomerId.setMinimumSize(new java.awt.Dimension(100, 20));
        txtCustomerId.setPreferredSize(new java.awt.Dimension(100, 20));
        txtCustomerId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCustomerIdKeyReleased(evt);
            }
        });

        txtName.setEditable(false);
        txtName.setMinimumSize(new java.awt.Dimension(100, 20));
        txtName.setPreferredSize(new java.awt.Dimension(100, 20));

        txtZipcode.setEditable(false);
        txtZipcode.setMinimumSize(new java.awt.Dimension(100, 20));
        txtZipcode.setPreferredSize(new java.awt.Dimension(100, 20));

        txtAddress.setEditable(false);
        txtAddress.setMinimumSize(new java.awt.Dimension(100, 20));
        txtAddress.setPreferredSize(new java.awt.Dimension(100, 20));

        txtCity.setEditable(false);
        txtCity.setMinimumSize(new java.awt.Dimension(100, 20));
        txtCity.setPreferredSize(new java.awt.Dimension(100, 20));

        btnSend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgOrders.png"))); // NOI18N
        btnSend.setText("Bestelling verzenden");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        btnSelectCustomerId.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgCustomer.gif"))); // NOI18N
        btnSelectCustomerId.setText("Selecteer...");
        btnSelectCustomerId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectCustomerIdActionPerformed(evt);
            }
        });

        lblCustomerID.setText("Klant ID:");
        lblCustomerID.setMinimumSize(new java.awt.Dimension(100, 20));
        lblCustomerID.setPreferredSize(new java.awt.Dimension(100, 20));

        lblName.setText("Naam:");
        lblName.setMinimumSize(new java.awt.Dimension(100, 20));
        lblName.setPreferredSize(new java.awt.Dimension(100, 20));

        lblAddress.setText("Adres:");
        lblAddress.setMinimumSize(new java.awt.Dimension(100, 20));
        lblAddress.setPreferredSize(new java.awt.Dimension(100, 20));

        lblZipcode.setText("Postcode:");
        lblZipcode.setMinimumSize(new java.awt.Dimension(100, 20));
        lblZipcode.setPreferredSize(new java.awt.Dimension(100, 20));

        lblCity.setText("Woonplaats:");
        lblCity.setMinimumSize(new java.awt.Dimension(100, 20));
        lblCity.setPreferredSize(new java.awt.Dimension(100, 20));

        txtNote.setColumns(20);
        txtNote.setRows(5);
        jScrollPane1.setViewportView(txtNote);

        lblNote.setText("Opmerking:");

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgBack.png"))); // NOI18N
        btnBack.setText("Terug");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                    .addComponent(lblNote)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmbPaymentmethod, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(btnBack)
                        .addGap(6, 6, 6)
                        .addComponent(btnSend))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCustomerId, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtAddress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblZipcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtZipcode, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCity, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE))
                            .addComponent(btnSelectCustomerId, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtCustomerId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(11, 11, 11)
                                .addComponent(lblAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(lblNote))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnSelectCustomerId)
                                .addGap(4, 4, 4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblZipcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtZipcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(11, 11, 11)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(cmbPaymentmethod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnBack)
                            .addComponent(btnSend))))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        if(txtCustomerId.getText().isEmpty() ||
                txtAddress.getText().isEmpty() ||
                txtCity.getText().isEmpty() ||
                txtName.getText().isEmpty() ||
                txtNote.getText().isEmpty() ||
                txtZipcode.getText().isEmpty()) {
            WinkelApplication.msgbox("Vul de klantgegevens in.");
        } else {
            customer_id = Integer.parseInt(txtCustomerId.getText());
            String note = txtNote.getText();
            String paymentmethod = cmbPaymentmethod.getSelectedItem().toString();
        
            WinkelApplication.getQueryManager().setOrder(WinkelApplication.getBasket(), customer_id, note, paymentmethod);
            WinkelApplication.getBasket().empty();
            WinkelApplication.getInstance().showPanel(new OrderSend());
        }
    }//GEN-LAST:event_btnSendActionPerformed
    
    //Code Start Bryan
    private void txtCustomerIdKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCustomerIdKeyReleased
        if(txtCustomerId.getText().isEmpty()) {
            txtName.setText("");
            txtAddress.setText("");
            txtZipcode.setText("");
            txtCity.setText("");
        } else if(!txtCustomerId.getText().isEmpty()) {
            int customer_id = Integer.parseInt(txtCustomerId.getText());
            Customer customer = WinkelApplication.getQueryManager().getCustomer(customer_id);
            
            txtName.setText(customer.getName());
            txtAddress.setText(customer.getAddress());
            txtZipcode.setText(customer.getZipcode());
            txtCity.setText(customer.getCity());
        }
    }//GEN-LAST:event_txtCustomerIdKeyReleased

    private void btnSelectCustomerIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectCustomerIdActionPerformed
        WinkelApplication.getInstance().showPanel(new CustomerOverview(true));
    }//GEN-LAST:event_btnSelectCustomerIdActionPerformed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        WinkelApplication.getInstance().showPanel(new CategoryList());
    }//GEN-LAST:event_btnBackActionPerformed
    //Code End Bryan
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnSelectCustomerId;
    private javax.swing.JButton btnSend;
    private javax.swing.JComboBox cmbPaymentmethod;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblCity;
    private javax.swing.JLabel lblCustomerID;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblNote;
    private javax.swing.JLabel lblZipcode;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtCity;
    private javax.swing.JTextField txtCustomerId;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextArea txtNote;
    private javax.swing.JTextField txtZipcode;
    // End of variables declaration//GEN-END:variables
}
